package com.boarsoft.common.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * 加密、解密Token工具类
 * @author xuhui.guo
 *
 */
@SuppressWarnings("restriction")
public class Base64Util {
	/**
	 * Base64加密
	 * @param val
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	public static String encode(String val) throws UnsupportedEncodingException {
		BASE64Encoder encoder = new BASE64Encoder();
		return encoder.encode(val.getBytes("utf-8")).replace("\n", "").replace("\r", "");
	}
	
	/**
	 * Base64解密
	 * @param val
	 * @return
	 * @throws IOException 
	 */
	public static String decode(String val) throws IOException {
		BASE64Decoder decoder = new BASE64Decoder();
		byte[] buffer = decoder.decodeBuffer(val);
		return new String(buffer, "utf-8");
	}	
}
